<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<div id="openWin" class="easyui-window" inline="true" closed="true" iconCls="icon-save" style="width:500px;height:580px;padding:5px;">
	<div class="easyui-layout" fit="true">
		<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">

			<input type="hidden" name="contractId" id="contractId" />
			<table>
				<tr>
					<td style="vertical-align: top;"><label>合同名称</label></td>
					<td>
						<input type="text" name="contractName" id="contractName" class="easyui-validatebox" required="true" style="width: 120px;" />
					</td>
					<td style="vertical-align: top;"><label>合同编号</label></td>
					<td>
						<input type="text" name="contractCode" id="contractCode" class="easyui-validatebox" required="true" style="width: 120px;"  />
					</td>
				</tr>				
				<tr>
					<td style="vertical-align: top;"><label>所属项目</label></td>
					<td>
						<s:hidden name="projectId" />
						<input type="text" name="displayProjectName" id="displayProjectName" 
							class="easyui-validatebox" required="true" style="width: 120px;" 
							readonly onclick="popUpWin('projectId', 'displayProjectName');" />
					</td>
					<td style="vertical-align: top;"><label>创建人</label></td>
					<td>
						<s:select id="createUserId" name="createUserId" list="#request.employeeInfoList" listKey="employeeId" listValue="employeeName" theme="simple" style="width: 125px;" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>合同金额</label></td>
					<td colspan="3">
						<input type="text" name="amount" id="amount" class="easyui-validatebox" required="true" style="width: 120px;"  />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>付款方式</label></td>
					<td colspan="3">
						<div>
							<s:select id="paymentId" name="paymentId" list="#request.paymentInfoList" listKey="paymentId" listValue="paymentName" theme="simple" onchange="paymentChange(this);" style="width: 150px; float: left;" />
							<input type="text" name="paymentRemark" id="paymentRemark"  style="display: none; width: 150px; float: left; margin-left: 2px;" />
							<input type="button" value="增加" id="paymentAddBtn" name="paymentAddBtn" onclick="addPayment(this);" style="float: left; margin-left: 2px;" />
							<input type="button" value="删除" id="paymentDelBtn" name="paymentDelBtn" onclick="delPayment(this);" style="display: none; margin-left: 2px;" />
						</div>
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>建立日期</label></td>
					<td colspan="3">
						<input type="text" style="width: 125px;" id="createTime" name="createTime" />
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>合同内容</label></td>
					<td colspan="3">
						<textarea rows="3" cols="35" name="contractContent" id="contractContent" ></textarea>
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>附件</label></td>
					<td colspan="3">
						<input type="file" id="uploadFile" name="uploadFile">
					</td>
				</tr>
				<tr>
					<td style="vertical-align: top;"><label>备注</label></td>
					<td colspan="3">
						<textarea rows="3" cols="35" name="remark" id="remark" ></textarea>
					</td>
				</tr>
			</table>
		</div>
		<div region="south" border="false" style="text-align:right;padding:5px 0;">
			<a class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" id="btn-save" onclick="save();">保存</a>
			<a class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)" onclick="$('#openWin').window('close');">关闭</a>
		</div>
	</div>
</div>

<script>

 $(function() {
  
	  initDatePicker("createTime");
	  
  });
 
 function save() {
	  if ($('#contractName').validatebox('isValid')
			  && $('#contractCode').validatebox('isValid')
			  && $('#amount').validatebox('isValid')
			  && $('#displayProjectName').validatebox('isValid')
			  && checkAmount($('#amount').val())) {
		  $("#queryForm").submit();
	  }
 }
 
 function openAddForm() {
	  $("#contractId").val("");
	  
	  openWin('新建合同', 'contract_addContractAction.action');
 }
 
 function openWin(title, action) {
	  $("#queryForm").attr("action", action);
	  
	  winInit();
	  
	  $('#openWin').window({
			title: title,
			modal: true,
			shadow: false,
			closed: false,
			collapsible: false,
			minimizable: false,
			maximizable: false,
			width: 500,
			height: 420
		});
 }
 
function openViewForm(selectId) {

  showData(selectId);
  
  openWin('查看合同', '');
  
  $("#contractName").attr("disabled", "true");
  $("#contractCode").attr("disabled", "true");
  $("#projectId").attr("disabled", "true");
  $("#displayProjectName").attr("disabled", "true");
  $("#createUserId").attr("disabled", "true");
  $("#amount").attr("disabled", "true");
  $("#createTime").attr("disabled", "true");
  $("#contractContent").attr("disabled", "true");
  $("#remark").attr("disabled", "true");
  $("#contractId").attr("disabled", "true");
  
	$("select[name='paymentId']").each(function(i){
		$(this).attr("disabled", "true");
	});
	$("input[name='paymentRemark']").each(function(i){
		$(this).attr("disabled", "true");
	});
	$("input[name='paymentAddBtn']").each(function(i){
		$(this).css("display", "none"); 
	});
	$("input[name='paymentDelBtn']").each(function(i){
		$(this).attr("disabled", "true");
		$(this).css("display", "none"); 
	});
	
	$("#btn-save").css("display", "none"); 
}

function openModifyForm() {
	  
	  var rows = $('#dataTable').datagrid('getSelections');
	  if (rows.length <= 0) {
		  $.messager.alert('信息','请选择一条记录！','info');
		  return;
	  } else if (rows.length > 1) {
		  $.messager.alert('信息','只能选择一条记录！','info');
		  return;
	  }
	  
	  showData(rows[0].contractId);
	  
	  openWin('修改合同', 'contract_modifyContractAction.action');
}

 function showData(selectId) {
  
  var url = "contract_getContractAction.action?contractId="+selectId;
  $.getJSON(url, function(data){
	  
		 if (data != null) {
			  $("#contractName").val(data.contractInfo.contractName);
			  $("#contractCode").val(data.contractInfo.contractCode);
			  $("#projectId").val(data.contractInfo.projectId);
			  $("#displayProjectName").val(data.contractInfo.projectName);
			  $("#createUserId").val(data.contractInfo.createUserId);
			  $("#amount").val(data.contractInfo.amount);
			  
			  var createTime;
			  if (data.contractInfo.createTime != null
					  && data.contractInfo.createTime != '') {
				  createTime = new Date(data.contractInfo.createTime).format("yyyy-MM-dd");
			  } else {
				  createTime = "";
			  }
			  $("#createTime").datebox('setValue', createTime);
			  $("#contractContent").val(data.contractInfo.contractContent);
			  $("#remark").val(data.contractInfo.remark);
			  $("#contractId").val(data.contractInfo.contractId);
			  
			  // 初始化回1个支付方式
			  var paymentSel = $("select[name='paymentId']");
			  for (var i = paymentSel.length -1; i > 0; i--) {
				  var paymentDiv = $(paymentSel[i]).parent();
				  paymentDiv.remove();
			  }
			  
			  
			  for(var i = 0; i < data.contractInfo.cpList.length; i++) {
				  
				  if (i > 0) {
					  addPayment($("select[name='paymentId']")[i-1]);
				  }
				  
				  var cp = data.contractInfo.cpList[i];
				  $($("select[name='paymentId']")[i]).val(cp.paymentId);
				  $($("input[name='paymentRemark']")[i]).val(cp.paymentRemark);
				  
				  paymentChange($("select[name='paymentId']")[i]);
			  }
		
		 }
	});
}
 
 function addPayment(obj) {
	  var div = $(obj).parent();
	  var cloneDiv = div.clone();
	  // select obj
	  $(cloneDiv.children("select")[0]).css("width", "");
	  // remark text
	  $(cloneDiv.children("input")[0]).val("");
	  $(cloneDiv.children("input")[0]).css("display", "none");
	
	  div.after(cloneDiv);
	  
	  showPaymentDelBtn();
 }
 
 function delPayment(obj) {
	  var div = $(obj).parent();
	  div.remove();
	  showPaymentDelBtn();
 }
 
 function paymentChange(obj) {
	  if (obj.value == 4 || obj.value == 10) {
		  $(obj).css("width", "60px");
		  $(obj).next("#paymentRemark").css("display", "");
	  } else {
		  $(obj).css("width", "");
		  $(obj).next("#paymentRemark").css("display", "none");
	  }
 }
 
 function showPaymentDelBtn() {
	// 至少有1个支付方式
	  var delBtn = $("input[name='paymentDelBtn']");
	  if (delBtn.length > 1) {
		  for (var i = 0; i < delBtn.length; i++) {
			// del btn
			  $($(delBtn[i])).css("display", "");
		  } 
	  } else {
		  $($(delBtn[0])).css("display", "none"); 
	  }
 }
 
 
 function winInit() {
	 $("#contractName").removeAttr("disabled");
	  $("#contractCode").removeAttr("disabled");
	  $("#projectId").removeAttr("disabled");
	  $("#displayProjectName").removeAttr("disabled");
	  $("#createUserId").removeAttr("disabled");
	  $("#amount").removeAttr("disabled");
	  $("#createTime").removeAttr("disabled");
	  $("#contractContent").removeAttr("disabled");
	  $("#remark").removeAttr("disabled");
	  $("#contractId").removeAttr("disabled");
	  
		$("select[name='paymentId']").each(function(i){
			$(this).removeAttr("disabled");
		});
		$("input[name='paymentRemark']").each(function(i){
			$(this).removeAttr("disabled");
		});
		$("input[name='paymentAddBtn']").each(function(i){
			$(this).css("display", "block"); 
		});
		$("input[name='paymentDelBtn']").each(function(i){
			$(this).removeAttr("disabled");
			$(this).css("display", "block"); 
		});
		
		$('#btn-save').css('display', '');
		
		showPaymentDelBtn();
 }

</script>
